package com.neighbor.neighborhoodadmin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neighbor.neighborhoodadmin.pojo.Notifications;
import com.neighbor.neighborhoodadmin.dao.INotificationsDao;
import com.neighbor.neighborhoodadmin.pojo.dto.NotificationDTO;
import com.neighbor.neighborhoodadmin.service.INotificationsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.print.Pageable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统通知表 服务实现类
 * </p>
 *
 * @author song
 * @since 2025-09-22 09:11:23
 */
@Service
public class NotificationsServiceImpl extends ServiceImpl<INotificationsDao, Notifications> implements INotificationsService {
    @Autowired
    INotificationsDao notificationsDao;

    @Override
    public Map<String, Object> getAllNotifications(int page, int size) {
        int offset = (page - 1) * size;
        List<Notifications> notifications = notificationsDao.selectAll(offset, size);
        int total = notificationsDao.selectCount();
        Map<String, Object> result = new HashMap<>();
        result.put("notifications", notifications);
        result.put("total", total);
        result.put("pages", (int) Math.ceil((double) total / size));
        return result;
    }

    @Override
    public Notifications getNotificationById(Integer id) {
        return notificationsDao.selectById(id);
    }

    @Override
    public int createNotification(NotificationDTO dto) {
        Notifications notification = new Notifications();
        notification.setTitle(dto.getTitle());
        notification.setContent(dto.getContent());
        notification.setImages(dto.getImages());
        notification.setType(dto.getType());
        notification.setRelatedId(dto.getRelatedId());
        notification.setUserId(null); // 管理员创建全体通知
        notification.setIsRead((byte) 0); // 默认未读
        notification.setCreatedAt(new Date());
        return notificationsDao.insert(notification);
    }

    @Override
    public int updateNotification(Integer id, NotificationDTO dto) {
        Notifications notification = notificationsDao.selectById(id);
        if (notification == null) {
            return 0;
        }
        notification.setTitle(dto.getTitle());
        notification.setContent(dto.getContent());
        notification.setType(dto.getType());
        notification.setImages(dto.getImages());
        notification.setRelatedId(dto.getRelatedId());
        return notificationsDao.update(notification);
    }

    @Override
    public int deleteNotification(Integer id) {
        return notificationsDao.delete(id);
    }
}
